<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BoxGiftOrder;
// use App\Models\BoxGiftboxitem;
use Dcat\Admin\Support\JavaScript;
use Illuminate\Support\Str;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BoxGiftOrderController extends AdminController
{
    /**
     * Make a grid builder.
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BoxGiftOrder(), function (Grid $grid) {
            $titles = ['id' => 'ID', 'username' => '名称', 'commodity' => '标题', 'commodityContent' => ' 详情', 'city' => '城市', 'address' => '地址'];

            $grid->export()->titles($titles)->disableExportAll()->filename('礼盒订单');
            $grid->disableCreateButton();
            $grid->disableEditButton(false);
            $grid->disableViewButton(true);
            $grid->quickSearch('username', 'commodity', 'BoxGiftboxitem.details', 'content')->placeholder('搜索...')->auto(true);
            // $grid->fixColumns(1);
            $grid->column('id')->sortable();
            // 使用关联关系获取用户姓名
            $grid->column('username', '姓名')->display(function ($username) {
                return $username;
            })->width('6%');
            // $grid->column('commodity_id');
            $grid->column('commodity', '商品标题')->display(function ($title) {
                $limit = 8; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($title, 'utf-8') > $limit) {
                    $title = mb_substr($title, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $title; // 返回关联表的  字段
            });

            // $grid->column('', '商品图')->image('http://laraveDact.com/uploads', 100, 100);

            $grid->column('commodityimage', '商品图')->display(function ($avatar) {
                $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
                return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
            });

            $grid->column('commodityContent', '商品详情')->display(function ($details) {
                $limit = 20; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($details, 'utf-8') > $limit) {
                    $details = mb_substr($details, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $details; // 返回关联表的  字段
            });

            $grid->column('city', '城市');
            $grid->column('address', '详细地址')->display(function ($address) {
                $limit = 20; // 设置要显示的字符数
                $ellipsis = '...'; // 设置省略号
                if (mb_strlen($address, 'utf-8') > $limit) {
                    $address = mb_substr($address, 0, $limit, 'utf-8') . $ellipsis;
                }
                return $address; // 返回关联表的  字段
            });

            $grid->column('opayment_Amount')->width('8%');
            $grid->column('status')->using(['1' => '支付', '2' => '未支付', '3' => '已到货'])->badge([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default    
                1 => 'primary',
                2 => 'danger',
                3 => 'success',
            ])->width('8%');
            $grid->column('date')->width('10%');




            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                // Grid\Column\Filter\Between::make()->date();
                // $filter->day('date');
                $filter->between('date', '日期范围')->date();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    // protected function detail($id)
    // {
    //     return Show::make($id, new BoxGiftOrder(), function (Show $show) {
    //         $show->field('id');
    //         $show->field('opayment_Amount');
    //         $show->field('city', '城市');
    //         $show->field('address', '详细地址');
    //         // $show->image('commodityimage')->as(function ($avatar) {
    //         //     $domain = Str::startsWith($avatar, 'uploads') ? 'http://wxy.jingyi.icu/' : 'http://laraveDact.com/uploads/';
    //         //     return "<img data-action='preview-img' src='{$domain}{$avatar}' width='80' height='80' class='img img-thumbnail'>";
    //         // });
    //         // $show->field('website', '网站')->display(function ($value) {
    //         //     return "<a href='$value' target='_blank'>$value</a>";
    //         // });
    //         // $show->field('content');
    //         $show->field('status');
    //         $show->field('date');
    //     });
    // }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BoxGiftOrder(), function (Form $form) {
            $form->disableViewButton(true);
            $form->display('id')->required()->disable();
            $form->text('username', '姓名')->required()->disable();
            $form->text('city', '城市')->required()->disable();
            $form->text('address', '详细地址')->required()->disable();
            $form->image('commodityimage', '商品图')->disable()->disk('public')->accept('jpg,png,gif,jpeg')->maxSize(1024)->required()->autoUpload()->help('大小不要超过1024K');
            $form->number('opayment_Amount')->disable()->required();
            $form->radio('status')->required()->disable()->options([1 => '已支付', 2 => '未支付', 3 => '已完成'])->default();
            // $form->editor('commodityContent', '商品详情')->height('200')->options([
            //     'toolbar' => [],
            //     'setup' => JavaScript::make(
            //         <<<JS
            //     function (editor) {
            //         console.log('编辑器初始化成功', editor)
            //     }
            //     JS
            //     ),
            // ]);
            $form->date('date')->format('YYYY-MM-DD')->disable();
            $form->markdown('commodityContent', '商品详情')->disable()->options([
                'onpreviewing' => JavaScript::make(
                    <<<JS
            function() {
                // console.log("onpreviewing =>", this, this.id, this.settings);
                // on previewing you can custom css .editormd-preview-active
            }
            JS
                ),
            ])->disk('public')->required();

            // $form->editor('content', '支付协议')->height('200')->options([
            //     'toolbar' => [],
            //     'setup' => JavaScript::make(
            //         <<<JS
            //     function (editor) {
            //         console.log('编辑器初始化成功', editor)
            //     }
            //     JS
            //     ),
            // ]);
        });
    }
}
